﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace 提交管理平台V1
{
    public class UserInfo
    {
        public struct ExportTemplateStruct
        {
            public string TemplateName;
            public string[] Fields;
        }

        public struct UserSettingsStruct
        {
            public string[] Fields;
            public List<ExportTemplateStruct> Templates;
            public int MaxRecordCount;
            public int FontSize;
            public bool FieldsNotSet;
            public bool TemplatesNotSet;
        }

        SqlTools sqt;

        string strSelectUserInfo = "select Unuser 用户停用,UserCharacter 用户角色 from UserList where Mname='TJGL' and Uname='@用户名称' and Pwd='@密码'";
        string strSelectUserSetting = "select Settings 用户设置 From UserSettings where Mname='TJGL' and Uname='@用户名称' and Stname='@设置名称'";
        string strInsertUserSetting = "insert into UserSettings (Uname,Mname,Stname,Settings) values ('@用户名称','TJGL','@设置名称','@用户设置')";
        string strUpdateUserSetting = "update UserSettings set Settings='@用户设置' where Mname='TJGL' and Uname='@用户名称' and Stname='@设置名称'";
        string strCheckUserPermission="select 权限 from 角色权限表 where 角色名称='@角色名称' and 权限='@权限'";

        private string _UserName;
        private string _Character;
        private string _ERPUserName;
        private bool _LoginSuccess;
        private string _LoginErrMsg;
        private UserSettingsStruct _UserSettings;

        /// <summary>
        /// 获取用户名称
        /// </summary>
        public string UserName
        {
            get
            {
                return _UserName;
            }
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        public string Character
        {
            get
            {
                return _Character;
            }
        }

        public string ERPUserName
        {
            get
            {
                return _ERPUserName;
            }
        }

        /// <summary>
        /// 获取用户登录是否成功
        /// </summary>
        public bool LoginSuccess
        {
            get
            {
                return _LoginSuccess;
            }
        }

        /// <summary>
        /// 获取用户登录错误信息
        /// </summary>
        public string LoginErrMsg
        {
            get
            {
                return _LoginErrMsg;
            }
        }

        /// <summary>
        /// 获取用户设置，包括字段设置、导出模板、最大记录条数等
        /// </summary>
        public UserSettingsStruct UserSettings
        {
            get
            {
                return _UserSettings;
            }
        }

        public UserInfo(string strTmpUserName, string strTmpPassword)
        {
            _UserName = strTmpUserName;
            _Character = string.Empty;
            _UserSettings = new UserSettingsStruct();
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1 .Properties .Settings .Default .Host , "DefualtDataBase"));
            DataTable tmpdt=sqt.GetData (strSelectUserInfo.Replace("@用户名称", strTmpUserName)
                                                                                           .Replace("@密码", strTmpPassword));
            if (tmpdt.Rows.Count == 0)
            {
                _LoginSuccess = false;
                _LoginErrMsg = "用户不存在或密码不正确！";
            }
            else
            {
                if (GeneralTools.GetBoolean(tmpdt.Rows[0]["用户停用"], true))
                {
                    _LoginSuccess = false;
                    _LoginErrMsg = "用户已停用！";
                }
                else
                {
                    _LoginSuccess = true ;
                    _LoginErrMsg = string.Empty;
                    _Character = GeneralTools.GetString(tmpdt.Rows[0]["用户角色"], string.Empty);
                    iniUserSettings();
                }
            }
        }

        /// <summary>
        /// 初始化用户设置
        /// </summary>
        public void iniUserSettings()
        {
            //字段设置
            string strTmpFields = GetUserSetting("字段设置");
            if (strTmpFields.Equals(string.Empty))
            {
                _UserSettings.Fields = null;
                _UserSettings.FieldsNotSet = true;
            }
            else
            {
                _UserSettings.Fields = strTmpFields.Split('|');
                _UserSettings.FieldsNotSet = false;
            }
            //模板设置
            string strTmpTemplateName = string.Empty;
            string strTmpTemplateFields = string.Empty;
            _UserSettings.TemplatesNotSet = true ;
            for (int i = 1; i <= 10; i++)
            {
                strTmpTemplateName = "导出模板" + i.ToString();
                strTmpTemplateFields = GetUserSetting(strTmpTemplateName);
                if (!strTmpTemplateFields.Equals(string.Empty))
                {
                    ExportTemplateStruct tmpExportTemplate = new ExportTemplateStruct();
                    tmpExportTemplate.TemplateName = strTmpTemplateName;
                    tmpExportTemplate.Fields = strTmpTemplateFields.Split('|');
                    _UserSettings.TemplatesNotSet = false;
                }
            }
            //最大记录条数
            _UserSettings.MaxRecordCount = GeneralTools.GetInt(GetUserSetting ("最大记录条数"), 100);
            //字号
            _UserSettings.FontSize = GeneralTools.GetInt(GetUserSetting("字号"), 10);
        }

        /// <summary>
        /// 获取当前用户指定设置项目的用户设置
        /// </summary>
        /// <param name="strTmpSettingName">设置名称</param>
        /// <returns>用户设置</returns>
        public string GetUserSetting(string strTmpSettingName)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            string strSetting = GeneralTools.GetString(sqt.DoCmdScalarData(strSelectUserSetting.Replace("@用户名称", _UserName)
                                                                                                                                                        .Replace("@设置名称", strTmpSettingName)), string.Empty).Trim ();
            return strSetting;
        }

        /// <summary>
        /// 更新当前用户指定设置项目的用户设置
        /// </summary>
        /// <param name="strTmpSettingName">设置名称</param>
        /// <param name="strTmpSettings">用户设置</param>
        /// <returns>更新是否成功</returns>
        public bool UpdateUserSetting(string strTmpSettingName, string strTmpSettings)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            if (sqt.RecordExist(strSelectUserSetting.Replace("@用户名称", _UserName)
                                                                           .Replace("@设置名称", strTmpSettingName)))
            {
                if (sqt.DoCmdExecuteNonQuery(strUpdateUserSetting.Replace("@用户名称", _UserName)
                                                                                                    .Replace("@设置名称", strTmpSettingName)
                                                                                                    .Replace("@用户设置", strTmpSettings)) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (sqt.DoCmdExecuteNonQuery(strInsertUserSetting.Replace("@用户名称", _UserName)
                                                                                                    .Replace("@设置名称", strTmpSettingName)
                                                                                                    .Replace("@用户设置", strTmpSettings)) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 检查用户是否具有指定权限
        /// </summary>
        /// <param name="strPermission">权限</param>
        /// <returns>是/否</returns>
        public bool CheckPermission(string strPermission)
        {
            return sqt.RecordExist(strCheckUserPermission.Replace("@角色名称", Character)
                                                                                        .Replace("@权限", strPermission));
        }
    }
}
